<template>
  <div class="login-container">
    <!-- 左侧渐变背景 -->
    <div class="login-left">
      <div class="login-left-content">
        <div class="login-logo">
          <div class="logo-circle"><img class="logo-circle-img" src="@/assets/images/erp.jpg" alt=""></div>
          <h1 class="system-title">erp业务系统</h1>
          <p class="system-desc">高效 · 智能 · 安全</p>
        </div>
      </div>
    </div>
    
    <!-- 右侧登录卡片 -->
    <div class="login-right">
      <div class="login-card">
        <div class="login-header">
          <h1 class="login-title">登录系统</h1>
          <p class="login-subtitle">欢迎回来，请输入您的账号信息</p>
        </div>
        
        <el-alert
          title="示例账号：admin / 密码：123456"
          type="info"
          :closable="false"
          class="login-alert"
        />
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          
          <el-form-item prop="captcha">
            <div class="captcha-container">
              <el-input
                v-model="loginForm.captcha"
                placeholder="验证码"
              />
              <div class="captcha-img" @click="refreshCaptcha">
                {{ captcha }}
              </div>
            </div>
          </el-form-item>
          
          <div class="login-options">
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
          
          <el-form-item>
            <el-button
              type="primary"
              native-type="submit"
              :loading="loading"
              class="login-button"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/store/auth'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

// 验证码
const captcha = ref('')
const generateCaptcha = () => {
  return Math.floor(1000 + Math.random() * 9000).toString()
}

// 刷新验证码
const refreshCaptcha = () => {
  captcha.value = generateCaptcha()
  localStorage.setItem('captcha', captcha.value)
}

// 表单数据
const loginForm = reactive({
  username: '',
  password: '',
  captcha: '',
  remember: false
})

// 表单校验规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 4, message: '验证码长度为 4 位', trigger: 'blur' }
  ]
}

// 加载状态
const loading = ref(false)

// 登录表单引用
const loginFormRef = ref()

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    if (!loginFormRef.value) return
    
    await loginFormRef.value.validate()
    
    // 验证码验证
    const storedCaptcha = localStorage.getItem('captcha')
    if (loginForm.captcha !== storedCaptcha) {
      ElMessage.error('验证码错误')
      refreshCaptcha()
      return
    }
    
    // 模拟API请求
    loading.value = true
    
    setTimeout(() => {
      if (loginForm.username === 'admin' && loginForm.password === '123456') {
        // 登录成功
        authStore.login({
          id: 1,
          name: 'Admin',
          avatar: '',
          token: 'mock-token-123456'
        })
        
        // 跳转到首页或重定向页面
        const redirectPath = route.query.redirect as string || '/'
        router.push(redirectPath)
        
        ElMessage.success('登录成功')
      } else {
        // 登录失败
        ElMessage.error('用户名或密码错误')
        refreshCaptcha()
      }
      
      loading.value = false
    }, 1000)
  } catch (error) {
    console.error('登录表单验证失败', error)
  }
}

// 初始化
onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

.login-left {
  flex: 1.2;
  background: url('@/assets/images/tech-background.jpg') no-repeat center center;
  background-size: cover;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(18, 30, 115, 0.6);
}

.login-left-content {
  position: relative;
  z-index: 1;
  text-align: center;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: white;
}

.logo-circle {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.15);
  border: 3px solid rgba(255, 255, 255, 0.3);
  margin-bottom: 24px;
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
}
.logo-circle-img{
  width: 100%;
  border-radius: 50%;
}

.system-title {
  font-size: 38px;
  font-weight: 600;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  margin-bottom: 16px;
}

.system-desc {
  font-size: 18px;
  opacity: 0.9;
  letter-spacing: 4px;
}

.login-right {
  flex: 0.8;
  background-color: #f9fafc;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 420px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  color: #303133;
  margin-bottom: 12px;
  font-weight: 600;
}

.login-subtitle {
  font-size: 14px;
  color: #606266;
}

.login-alert {
  margin-bottom: 25px;
  border-radius: 4px;
}

.captcha-container {
  display: flex;
  gap: 12px;
}

.captcha-img {
  width: 120px;
  height: 40px;
  background: linear-gradient(135deg, #1a73e8 0%, #3f51b5 100%);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  user-select: none;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.captcha-img:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 4px;
  background: linear-gradient(135deg, #1a73e8 0%, #3f51b5 100%);
  border: none;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(26, 115, 232, 0.3);
}

@media (max-width: 992px) {
  .login-left {
    flex: 1;
  }
  
  .login-right {
    flex: 1;
  }
}

@media (max-width: 768px) {
  .login-left {
    display: none;
  }
  
  .login-right {
    flex: 1;
    padding: 20px;
  }
  
  .login-card {
    width: 100%;
    max-width: 420px;
  }
}
</style> 